package com.pomr.controller.synthesize;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.model.menu.EquipmentKind;
import com.pomr.model.synthesize.Equipment;
import com.pomr.model.sys.User;
import com.pomr.service.synthesize.EquipmentService;
import com.pomr.util.common.CommonData;
import com.pomr.util.common.JWT;
/**
 * 设备管理
 * @author Administrator
 *
 */
import com.pomr.util.common.ResponseData;
import com.pomr.util.page.Page;
import com.pomr.util.page.PageParam;
@Controller
@RequestMapping("/equipment")
public class EquipmentController {
	@Autowired
	@Qualifier("equipmentServiceImpl")
	private EquipmentService EquipmentService;
	
	/**
	 * 添加前查询
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-addbf",method=RequestMethod.GET)
	@ResponseBody
	public ResponseData addEquipmentBf(HttpServletRequest request) throws Exception{
		List<EquipmentKind> kinds = (List<EquipmentKind>) CommonData.data.get("EquipmentKind");
		ResponseData pd = ResponseData.ok();
		pd.putDataValue("kinds", kinds);
		return pd;
	}
	
	@RequestMapping(value="/inter-add",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData addEquipment(@RequestBody Equipment equipment,HttpServletRequest request){
		ResponseData pd = null;
		try{
			User user = JWT.findUserByToken(request);
			equipment.setUser(user);
			if(EquipmentService.saveEquipment(equipment)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-search",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<Equipment>> searchEquipments(PageParam param, HttpServletRequest request){
		try{
			return EquipmentService.searchEquipmentsByCondition(param.getParams().get("name"), param.getPage(), param.getLimit());
		}catch (Exception e) {
			e.printStackTrace();
			return new Page<List<Equipment>>(400, "数据获取异常");
		}
	}
	
	@RequestMapping(value="/inter-delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteEquipments(@RequestBody Map<String, String> map,HttpServletRequest request){
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			if(ids == null || ids.trim().equals("")){
				pd = ResponseData.custom("未上传删除信息");
			}else{
				if(EquipmentService.deleteEquipmentsByIds(ids.split(","))){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.fail();
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	/**
	 * 通过id获取设备信息
	 * @return
	 */
	@RequestMapping(value="/inter-searchone",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData searchEquipment(@RequestBody Map<String, String> map,HttpServletRequest request){
		ResponseData pd = null;
		try{
			String id = map.get("id");
			if(id == null || id.trim().equals("")){
				pd = ResponseData.custom("未获取到设备唯一编号");
			}else{
				Equipment e = EquipmentService.searchEquipmentById(id);
				if(e == null){
					pd = ResponseData.custom("未获取到对应的设备信息");
				}else{
					pd = ResponseData.ok();
					pd.putDataValue("equipment", e);
				}
			}
			
		}catch (Exception e) {
			// TODO: handle exception
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-edit",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData editEquipment(@RequestBody Equipment equipment){
		ResponseData pd = null;
		try {
			if(equipment==null || equipment.getId()==null){
				pd = ResponseData.custom("未获取到设备唯一编号");
			}else{
				if(EquipmentService.editEquipmentById(equipment)){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.fail();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			pd = ResponseData.exceptionError();
		}
		return pd;
	}

}
